﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ESOL.Common.Entity;
using ESOL.Business.Mst;
using ESOL.WinUI.UICommon;
using ESOL.Common;
using ESOL.Business.Bpt;
using ESOL.Business.Prod;
using ESOL.Business.Sec;
using ESOL.WinUI.Proj;

namespace ESOL.WinUI.Bpt
{
    public partial class frmMaintainBlueprintChanges : BaseForm.frmBaseDialogyBusinessForm
    {
        #region Variable
        public BptDrawBlueprintChangesEntity SelectBptDrawBlueprintChanges { get; set; }
        public string AppPlanItemId { get; set; }
        private List<BptDrawBlueprintChangesItemEntity> DeleteList = new List<BptDrawBlueprintChangesItemEntity>();
        #endregion

        #region Constructor
        public frmMaintainBlueprintChanges()
        {
            InitializeComponent();
        }
        #endregion

        #region Functions
        private void BindComboBox()
        {
            //UICommon.UICommonFunctions.BindComboboxByCommonItem(cmbStatus, "BPTC", false);
            UICommon.UICommonFunctions.BindGridComboboxByCommonItem(colChangType, "BPTCT", true);
        }
        private void BindDataGridDetail()
        {
            if (SelectBptDrawBlueprintChanges == null)
            {
                gvMain.DataSource = null;
                return;
            }
            DataTable dt = BptDrawBlueprintChangesItemBs.GetBptDrawBlueprintChangesItemByID(SelectBptDrawBlueprintChanges.DrawChangesId);
            if (dt == null)
            {
                gvMain.DataSource = null;
                return;
            }
            dt.AcceptChanges();
            gvMain.DataSource = dt;
            DeleteList = new List<BptDrawBlueprintChangesItemEntity>();

        }
        private void LodTask()
        {
            if (string.IsNullOrEmpty(AppPlanItemId) == false && ThisActionType == ActionType.New)
            {
                ProdPlanOrderEntity ppoEntity = new ProdPlanOrderEntity();
                ppoEntity = ProdPlanOrderBs.GetProdPlanOrderByAppItemID(AppPlanItemId);

                txtProdApplicationItemId.Text = ppoEntity.PlanOrderCode;
                txtProdApplicationItemId.Tag = ppoEntity;
                List<ProdPlanOrderItemEntity> listOrderItem = new List<ProdPlanOrderItemEntity>();
                listOrderItem = ProdPlanOrderItemBs.GetProdPlanOrderItemByOrderID(ppoEntity.PlanOrderId);
                if (listOrderItem.Count > 0)
                {
                    DataTable dt = (DataTable)gvMain.DataSource;
                    dt.Rows.Clear();
                    foreach (ProdPlanOrderItemEntity entity in listOrderItem)
                    {
                        if (gvMain.DataSource == null)
                        {
                            return;
                        }

                        DataRow dr = dt.NewRow();
                        dr["DRAW_CHANGES_ITEM_ID"] = Guid.NewGuid().ToString();
                        dr["DRAW_CHANGES_ID"] = SelectBptDrawBlueprintChanges.DrawChangesId;
                        dr["PRODUCT_ID"] = entity.ProductId;
                        if (!entity.ProductId.Equals(""))
                        {
                            MstProductEntity ProductEntity = MstProductBs.GetMstProductTypeByID(entity.ProductId);
                            dr["PRODUCT_CODE"] = ProductEntity.ProductCode;
                            dr["PRODUCT_NAME"] = ProductEntity.ProductName;
                            dr["PRODUCT_SPEC_IN"] = ProductEntity.ProductSpecIn;
                            dr["PRODUCT_SPEC_OUT"] = ProductEntity.ProductSpecOut;
                            dr["MATERIAL"] = ProductEntity.Material;
                            dr["DESCRIPTION"] = ProductEntity.Description;
                        }
                        dr["BLUEPRINT_ID"] = entity.BlueprintId;
                        dr["CHANGE_TYPE"] = "BPTCT01";
                        dr["PLAN_ORDER_ITEM_ID"] = entity.PlanOrderItemId;
                        dr["PRODUCT_ID"] = entity.ProductId;
                        dt.Rows.Add(dr);
                    }
                }
            }
        }
        private void SetFields()
        {
            BindDataGridDetail();
            if (SelectBptDrawBlueprintChanges == null)
            {
                return;
            }
            if (ThisActionType == ActionType.New)
            {

                txtDrawchangesCode.Text = "自动生成";
            }
            else
            {
                txtDrawchangesCode.Text = SelectBptDrawBlueprintChanges.DrawChangesCode;
            }
            if (string.IsNullOrEmpty(SelectBptDrawBlueprintChanges.ProdApplicationItemId) == false)//工单号码
            {
                ProdPlanOrderEntity Plorder = new ProdPlanOrderEntity();
                Plorder = ProdPlanOrderBs.GetProdPlanOrderByAppItemID(SelectBptDrawBlueprintChanges.ProdApplicationItemId);
                if (Plorder != null)
                {

                    txtProdApplicationItemId.Text = Plorder.PlanOrderCode;
                    txtProdApplicationItemId.Tag = Plorder;
                }
            }
            if (string.IsNullOrEmpty(SelectBptDrawBlueprintChanges.SponsorPerson) == false)//申请人
            {
                MstPersonEntity person = MstPersonBs.GetMstPersonByID(SelectBptDrawBlueprintChanges.SponsorPerson);
                if (person != null)
                {

                    txtSponsorPerson.Tag = person;
                    txtSponsorPerson.Text = person.NameCn + " (" + person.PersonCode + ")";
                }
            }
            if (string.IsNullOrEmpty(SelectBptDrawBlueprintChanges.LastmodifyBy) == false)//最后修改人
            {
                MstPersonEntity person = MstPersonBs.GetMstPersonByID(SelectBptDrawBlueprintChanges.LastmodifyBy);
                if (person != null)
                {

                    txtLastModifyBy.Tag = person;
                    txtLastModifyBy.Text = person.NameCn + " (" + person.PersonCode + ")";
                }
            }
            if (string.IsNullOrEmpty(SelectBptDrawBlueprintChanges.RecivePerson) == false)//接收人
            {
                MstPersonEntity person = MstPersonBs.GetMstPersonByID(SelectBptDrawBlueprintChanges.RecivePerson);
                if (person != null)
                {
                    txtRecivePerson.Tag = person;
                    txtRecivePerson.Text = person.NameCn + " (" + person.PersonCode + ")";
                }
            }
            //cmbStatus.SetSelectedValue(SelectBptDrawBlueprintChanges.Status);//状态
            txtRemarks.Text = SelectBptDrawBlueprintChanges.Remarks;//备注
            UICommonFunctions.SetTextBoxDate(txtsponsorDate, SelectBptDrawBlueprintChanges.SponsorDate, "yyyy-MM-dd hh:mm:ss");
            UICommonFunctions.SetTextBoxDate(txtReciveDate, SelectBptDrawBlueprintChanges.ReciveDate, "yyyy-MM-dd hh:mm:ss");
            UICommonFunctions.SetTextBoxDate(txtLastModifydate, SelectBptDrawBlueprintChanges.LastmodifyDate, "yyyy-MM-dd hh:mm:ss");
            UICommonFunctions.SetTextBoxPersonName(txtSponsorPerson, SelectBptDrawBlueprintChanges.SponsorPerson);
            UICommonFunctions.SetTextBoxPersonName(txtRecivePerson, SelectBptDrawBlueprintChanges.RecivePerson);
            UICommonFunctions.SetTextBoxPersonName(txtLastModifyBy, SelectBptDrawBlueprintChanges.LastmodifyBy);
        }
        private void SetEntity()
        {
            if (txtProdApplicationItemId.Tag != null && txtProdApplicationItemId.Tag.GetType() == typeof(ProdPlanOrderEntity))
            {
                SelectBptDrawBlueprintChanges.ProdApplicationItemId = ((ProdPlanOrderEntity)txtProdApplicationItemId.Tag).ApplicationItemId;
                SelectBptDrawBlueprintChanges.ProdOrderId = ((ProdPlanOrderEntity)txtProdApplicationItemId.Tag).PlanOrderId;
            }

            if (txtPlanItem.Tag != null && txtPlanItem.Tag.GetType() == typeof(SalesProdPlanApplicationItemEntity))
            {
                SelectBptDrawBlueprintChanges.ProdApplicationItemId = ((SalesProdPlanApplicationItemEntity)txtPlanItem.Tag).ApplicationItemId;
            }
            SelectBptDrawBlueprintChanges.DrawChangesCode = txtDrawchangesCode.Text;
            SelectBptDrawBlueprintChanges.Remarks = txtRemarks.Text;

            if (!txtRecivePerson.Text.Equals(""))
            {
                SelectBptDrawBlueprintChanges.RecivePerson = ((MstPersonEntity)txtRecivePerson.Tag).PersonId;
            }
            if (ThisActionType == ActionType.New)
            {
                SelectBptDrawBlueprintChanges.DrawChangesId = Guid.NewGuid().ToString();
                SelectBptDrawBlueprintChanges.Status = "BPTC01";
                SelectBptDrawBlueprintChanges.SponsorPerson = CommonParameter.CurrentUser.PersonId;
                SelectBptDrawBlueprintChanges.SponsorDate = DateTime.Now;
                SelectBptDrawBlueprintChanges.LastmodifyDate = DateTime.Now;
                SelectBptDrawBlueprintChanges.LastmodifyBy = CommonParameter.CurrentUser.PersonId;

            }
            if (ThisActionType == ActionType.Modify)
            {
                SelectBptDrawBlueprintChanges.Status = "BPTC01";
                SelectBptDrawBlueprintChanges.LastmodifyDate = DateTime.Now;
                SelectBptDrawBlueprintChanges.LastmodifyBy = CommonParameter.CurrentUser.PersonId;

            }
            if (ThisActionType == ActionType.Approve)
            {
                SelectBptDrawBlueprintChanges.ReciveDate = DateTime.Now;
                SelectBptDrawBlueprintChanges.RecivePerson = CommonParameter.CurrentUser.PersonId;
                SelectBptDrawBlueprintChanges.LastmodifyDate = DateTime.Now;
                SelectBptDrawBlueprintChanges.LastmodifyBy = CommonParameter.CurrentUser.PersonId;
            }
        }
        private bool CheckData()
        {
            if (string.IsNullOrEmpty(txtProdApplicationItemId .Text ))
            {
                MessageBox.Show("生产计划单号不能为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            if (string.IsNullOrEmpty (txtRemarks .Text))
            {
                MessageBox.Show("原因说明不能为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            return true;
        }
        private bool Save()
        {
            try
            {
                if (CheckData() == false)
                {
                    return false;
                }
                if (ThisActionType == ActionType.New)
                {
                    SetEntity();
                    DataTable dt = (DataTable)gvMain.DataSource;
                    bool isSuccessful = BptDrawBlueprintChangesBs.SaveBptDrawPrintChanges(true, SelectBptDrawBlueprintChanges, dt, DeleteList);
                    if (!isSuccessful)
                    {
                        MessageBox.Show("新增失败！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return false;
                    }
                }
                else if (ThisActionType == ActionType.Modify)
                {
                    SetEntity();
                    DataTable dt = (DataTable)gvMain.DataSource;
                    bool isSuccessful = BptDrawBlueprintChangesBs.SaveBptDrawPrintChanges(false, SelectBptDrawBlueprintChanges, dt, DeleteList);
                    if (!isSuccessful)
                    {
                        MessageBox.Show("修改失败！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return false;
                    }
                }
                else if (ThisActionType == ActionType.Approve)
                {
                    SetEntity();
                    bool isSuccessful = BptDrawBlueprintChangesBs.ApproveBptDrawPrintChanges(SelectBptDrawBlueprintChanges);
                    if (!isSuccessful)
                    {
                        MessageBox.Show("审核失败，请先检查更改工单是否都已关闭！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return false;
                    }
                }
                return true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
        }
        private void SendMessage(string sMessageTitle, string sSender, string sMessageBody, string MessageLevel)
        {
            MstMessageEntity message = new MstMessageEntity();
            message.MessageId = Guid.NewGuid().ToString();
            message.MessateTitle = sMessageTitle;
            message.MessageSender = sSender;
            message.MessageBody = sMessageBody;
            message.MessageLevel = MessageLevel;
            message.FormName = "ESOL.WinUI.Bpt.frmDrawBlueprintManage";
            message.MessageDate = DateTime.Now;
            List<MstPersonEntity> toList = SecUserBs.GetPersonIdByFormAndObject("ESOL.WinUI.Bpt.frmDrawBlueprintManage", "分配任务", "btnModify");
            MstMessageBs.SendMessage(message, toList);
        }
        #endregion

        private void frmMaintainBlueprintChanges_Load(object sender, EventArgs e)
        {
            BindComboBox();
            SetFields();
            LodTask();
        }

        private void txtProdApplicationItemId_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainProductPlanOrderQuery frm = new frmMaintainProductPlanOrderQuery();
            if (frm.ShowDialog() == DialogResult.OK && frm.SelectedProdPlanOrder != null)
            {
                txtProdApplicationItemId.Text = frm.SelectedProdPlanOrder.PlanOrderCode;
                txtProdApplicationItemId.Tag = frm.SelectedProdPlanOrder;
                List<ProdPlanOrderItemEntity> listOrderItem = new List<ProdPlanOrderItemEntity>();
                listOrderItem = ProdPlanOrderItemBs.GetProdPlanOrderItemByOrderID(frm.SelectedProdPlanOrder.PlanOrderId);
                if (listOrderItem.Count > 0)
                {
                    DataTable dt = (DataTable)gvMain.DataSource;
                    dt.Rows.Clear();
                    foreach (ProdPlanOrderItemEntity entity in listOrderItem)
                    {
                        if (gvMain.DataSource == null)
                        {
                            return;
                        }

                        DataRow dr = dt.NewRow();
                        dr["DRAW_CHANGES_ITEM_ID"] = Guid.NewGuid().ToString();
                        dr["DRAW_CHANGES_ID"] = SelectBptDrawBlueprintChanges.DrawChangesId;
                        dr["PRODUCT_ID"] = entity.ProductId;
                        if (!entity.ProductId.Equals(""))
                        {
                            MstProductEntity ProductEntity = MstProductBs.GetMstProductTypeByID(entity.ProductId);
                            dr["PRODUCT_CODE"] = ProductEntity.ProductCode;
                            dr["PRODUCT_NAME"] = ProductEntity.ProductName;
                            dr["PRODUCT_SPEC_IN"] = ProductEntity.ProductSpecIn;
                            dr["PRODUCT_SPEC_OUT"] = ProductEntity.ProductSpecOut;
                            dr["MATERIAL"] = ProductEntity.Material;
                            dr["DESCRIPTION"] = ProductEntity.Description;
                        }
                        dr["BLUEPRINT_ID"] = entity.BlueprintId;
                        dr["CHANGE_TYPE"] = "BPTCT01";
                        dr["PLAN_ORDER_ITEM_ID"] = entity.PlanOrderItemId;
                        dr["PRODUCT_ID"] = entity.ProductId;
                        dt.Rows.Add(dr);
                    }
                }
            }
        }

        protected override void Click_Save()
        {
            if (Save())
            {
                this.DialogResult = DialogResult.OK;
            }
        }
        protected override void Click_Approve()
        {

            if (SelectBptDrawBlueprintChanges == null)
            {
                return;
            }
            SelectBptDrawBlueprintChanges.Status = "BPTC03";
            if (Save())
            {
                SendMessage("新制图任务", CommonParameter.CurrentUser.PersonId, "有新的制图任务需要分配了；", MessageLevel.ML03.ToString());
                this.DialogResult = DialogResult.OK;
            }
        }
        protected override void Click_Reject()
        {
            if (SelectBptDrawBlueprintChanges == null)
            {
                return;
            }
            SelectBptDrawBlueprintChanges.Status = "BPTC04";
            if (Save())
            {
                this.DialogResult = DialogResult.OK;
            }
        }



        private void itemAdd_Click(object sender, EventArgs e)
        {
            if (gvMain.DataSource == null)
            {
                return;
            }
            DataTable dt = (DataTable)gvMain.DataSource;
            DataRow dr = dt.NewRow();
            dr["DRAW_CHANGES_ITEM_ID"] = Guid.NewGuid().ToString();
            dr["CHANGE_TYPE"] = "BPTCT03";
            dt.Rows.Add(dr);
        }

        private void itemDelete_Click(object sender, EventArgs e)
        {
            if (gvMain.DataSource == null)
            {
                return;
            }
            DataTable dt = gvMain.DataSource as DataTable;
            if (mSelectedRow != null)
            {
                dt.Rows.Remove(mSelectedRow);
                if (mSelectedRow.RowState != DataRowState.Added)
                {
                    BptDrawBlueprintChangesItemEntity entity = new BptDrawBlueprintChangesItemEntity(mSelectedRow);
                    entity.ChangeType = "BPTCT04";
                    DeleteList.Add(entity);
                }
            }
            
        }

        private void txtPlanItem_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainProductPlanApplicationItemQuery frm = new frmMaintainProductPlanApplicationItemQuery(true);
            if (frm.ShowDialog() == DialogResult.OK && frm.SelectedApplicationItemList != null && frm.SelectedApplicationItemList.Count > 0)
            {
                txtPlanItem.Text = frm.SelectedApplicationItemList[0].ProductSpecOut;
                txtPlanItem.Tag = frm.SelectedApplicationItemList[0];
            }
        }
        DataRow mSelectedRow;
        private void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            mSelectedRow = gridView1.GetDataRow(e.FocusedRowHandle);
        }




    }
}